#!/bin/bash 
set -e

TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage3}
SRC=${SRC:-/sources}

name=systemd
version=247

rm -rf $TMP
mkdir -p $TMP $LOG

tar xf $SRC/$name-$version.tar.gz -C $TMP

{ time \
   {

    cd $TMP/$name-$version
    
    patch -Np1 -i $SRC/$name-$version-upstream_fixes-1.patch
    
    ln -sf /bin/true /usr/bin/xsltproc
    
    tar -xf $SRC/systemd-man-pages-$version.tar.xz
    
    sed '181,$ d' -i src/resolve/meson.build
    
	sed -i 's/GROUP="render"/GROUP="video"/' rules.d/50-udev-default.rules.in

	mkdir -p build
	cd       build

	LANG=en_US.UTF-8                    \
	meson --prefix=/usr                 \
	      --sysconfdir=/etc             \
	      --localstatedir=/var          \
	      -Dblkid=true                  \
	      -Dbuildtype=release           \
	      -Ddefault-dnssec=no           \
	      -Dfirstboot=false             \
	      -Dinstall-tests=false         \
	      -Dkmod-path=/bin/kmod         \
	      -Dldconfig=false              \
	      -Dmount-path=/bin/mount       \
	      -Drootprefix=                 \
	      -Drootlibdir=/lib             \
	      -Dsplit-usr=true              \
	      -Dsulogin-path=/sbin/sulogin  \
	      -Dsysusers=false              \
	      -Dumount-path=/bin/umount     \
	      -Db_lto=false                 \
	      -Drpmmacrosdir=no             \
	      -Dhomed=false                 \
	      -Duserdb=false                \
	      -Dman=true                    \
	      -Dmode=release                \
		  -Ddocdir=/usr/share/doc/$name-$version \
		  ..
    
	LANG=en_US.UTF-8 ninja
		
	LANG=en_US.UTF-8 ninja install
		
	rm -f /usr/bin/xsltproc
		
	systemd-machine-id-setup
		
	systemctl preset-all
		
	systemctl disable systemd-time-wait-sync.service
		
	rm -f /usr/lib/sysctl.d/50-pid-max.conf
    

    }
} 2>&1 | tee $name.log
    
[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
